import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router);

let router = new Router({
  routes: [
    {
      path: '*',
      name: '404',
      component: () => import('@/components/page404'),  // 路由懒加载
    },
    {
      path: "/",
      redirect: "/login"
    },
    {
      path: '/home',
      name: 'Home',
      component: () => import('@/components/Home'),
      meta: {
        isLogin: true
      },
      //首页
      redirect: "/home/commodity_storage",
      children: [
        {
          path: 'basic_product_information',
          component: () => import("@/components/Basic_product_information"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "store_basic_settings",
          component: () => import("@/components/Store_basic_settings"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "employee_basics",
          component: () => import("@/components/Employee_basics"),
          meta: {
            isLogin: true

          },
        },

        {
          path: "supplier_base_settings",
          component: () => import("@/components/Supplier_base_settings"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "authority_management",
          component: () => import("@/components/Authority_management"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "system_settings",
          component: () => import("@/components/System_settings"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "change_password",
          component: () => import("@/components/Change_password"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "stone_foundation_setting",
          component: () => import("@/components/Product_basics/Stone_foundation_setting"),
          meta: {
            isLogin: true
          }
        }, {
          path: "product_basics",
          component: () => import("@/components/Product_basics/Product_basics"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "product_code_list",
          component: () => import("@/components/Product_basics/Product_code_list"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "wholesale_stone_price_management",
          component: () => import("@/components/Product_basics/Wholesale_stone_price_management"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "wholesale_sales_labor_management",
          component: () => import("@/components/Product_basics/Wholesale_sales_labor_management"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "permission_setting",
          component: () => import("@/components/Authority_management/Permission_setting"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "role_management",
          component: () => import("@/components/Authority_management/Role_management"),
          meta: {
            isLogin: true
          }
        },
        {
          path: "encoding_rule_settings",
          component: () => import("@/components/System_settings/Encoding_rule_settings"),
          meta: {
            isLogin: true
          }
        },
        //商品管理数据
        {
          path: "commodity_material",
          component: () => import("@/components/Commodity_management/Commodity_material"),
          meta: {
            isLogin: true
          }
        },
        {
          path:"purchase_returns",
          component:()=>import("@/components/Commodity_management/Purchase_returns"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"merchandising",
          component:()=>import("@/components/Commodity_management/Merchandising"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"sales_return",
          component:()=>import("@/components/Commodity_management/Sales_return"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"inbound_statistics",
          component:()=>import("@/components/Commodity_management/Inbound_statistics"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"distribution_out",
          component:()=>import("@/components/Commodity_management/Distribution_out"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"outbound_statistics",
          component:()=>import("@/components/Commodity_management/Outbound_statistics"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"store_transfer",
          component:()=>import("@/components/Commodity_management/Store_transfer"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"commodity_storage",
          component:()=>import("@/components/Commodity_management/Commodity_storage"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"rough_inventory",
          component:()=>import("@/components/Commodity_management/Rough_inventory"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"data_analysis",
          component:()=>import("@/components/Commodity_management/Data_analysis"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"profit_analysis",
          component:()=>import("@/components/Commodity_management/Profit_analysis"),
          meta:{
            isLogin:true
          }
        },
        {
          path:"store_return",
          component:()=>import("@/components/Commodity_management/Store_return"),
          meta:{
            isLogin:true
          }
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/components/Login'),
      meta: {
        isLogin: false
      }
    }
  ]
});

//判断是否登录  前置守卫
router.beforeEach((to, from, next) => {
  //获取用户登录成功后储存的登录标志
  let getFlag = Boolean(sessionStorage.getItem("isLogin"));
  // console.log(getFlag);
  //如果登录标志存在且为isLogin，即用户已登录
  if (getFlag === true) {
    //设置vuex登录状态为已登录
    // store.state.isLogin = true
    next();
    //如果已登录，还想想进入登录注册界面，则定向回首页
    // if (!to.meta.isLogin) {
    //   //iViewUi友好提示
    //   // iView.Message.error('请先退出登录')
    //   next({
    //     path: '/'
    //   })
    // }
    //如果登录标志不存在，即未登录
  } else {
    //用户想进入需要登录的页面，则定向回登录界面
    if (to.meta.isLogin) {
      next({
        path: '/login',
      });
      alert("请先登录");
      //iViewUi友好提示
      // iView.Message.info('请先登录')
      //用户进入无需登录的界面，则跳转继续
    } else {
      next()
    }
  }
});


export default router;
